/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package lsichrome;

/**
 *
 * @author indu.chawla
 */
import java.util.*;

public class tdm {
    private Vector<String> documents = new Vector<String>();
	private Vector<String> uniqueTerms = new Vector<String>();
	private int numDocuments;
         Vector<String> Result_Vector_Array[];
    //   String Result[][]= new String[50][50];
       
       private Vector<String> temp = new Vector<String>();
	private double[][] TERM_TERM_MATRIX;
        private double[][] TERM_DOCUMENT_MATRIX ;
	
	public Vector<String> getUniqueTerms(){
		return uniqueTerms; 
	}
	public Vector<String> getDocuments(){
		return documents;
	}
	public void setNumDocuments(int numDocuments){
		this.numDocuments = numDocuments;
		Result_Vector_Array = (Vector<String>[]) new Vector[numDocuments];
                 
	}
        public double[][] getTermDocumentMatrix(){
		return TERM_DOCUMENT_MATRIX;
	}
        
        public double[][] getTermTermMatrix(){
		return TERM_TERM_MATRIX;
	}
	
	public void update(String document, Vector<String> terms, int index){
                Result_Vector_Array[index]=new Vector<String>();
                Result_Vector_Array[index].addAll(terms);
		documents.addElement(document); 
		uniqueTerms.addAll(terms);
		uniqueTerms = new Vector<String>(new LinkedHashSet<String>(uniqueTerms));
                          
                System.out.println("index"  +index);
                for(int i=0;i<Result_Vector_Array.length;i++){
                    
                    System.out.println( " Result_Vector_Array "  + Result_Vector_Array[i]);
                }
	}
	
	
	

public void processTermDocumentMatrix(){
    
    
    Collections.sort(uniqueTerms);
    Enumeration VEnum=uniqueTerms.elements();
                System.out.println(" uniqueterms");
                while( VEnum.hasMoreElements())
                        System.out.print(VEnum.nextElement()+" ");
                        System.out.println();
		TERM_TERM_MATRIX = new double[uniqueTerms.size()][uniqueTerms.size()];
		for(int i=0;i<(Result_Vector_Array.length)-1;i++){                   
                     
			Vector<String> identifiers = Result_Vector_Array[i];
			for(int j=0;j <identifiers.size();j++){
				String identifier = identifiers.elementAt(j);
				int index = uniqueTerms.lastIndexOf(identifier);
                                for(int p=0;p<identifiers.size();p++){
                                    String identifier1 = identifiers.elementAt(p);
				int index1 = uniqueTerms.lastIndexOf(identifier1);
				TERM_TERM_MATRIX[index][index1] = TERM_TERM_MATRIX[index][index1] + 1;  
			}
                }
                }
                        System.out.println( "TERM_TERM_MATRIX");
                        for(int k=0;k<uniqueTerms.size();k++){
                            System.out.print(  uniqueTerms.get(k));
			for(int m=0;m<uniqueTerms.size();m++){
			System.out.print( TERM_TERM_MATRIX[k][m]  +"    " );
                        } //m for
                       System.out.println("  ");
                        }
                       
                       TERM_DOCUMENT_MATRIX = new double[uniqueTerms.size()][documents.size()];
                
                       for(int i=0;i<Result_Vector_Array.length;i++){
                    Vector<String> identifiers = Result_Vector_Array[i];
                 for(int j=0;j<identifiers.size();j++){
				String identifier = identifiers.elementAt(j);
				int index = uniqueTerms.lastIndexOf(identifier);
				TERM_DOCUMENT_MATRIX[index][i] = TERM_DOCUMENT_MATRIX[index][i] + 1;  
			//System.out.print("identifiers" + identifiers.elementAt(j));
                 } } 
                       System.out.println("TERM_DOCUMENT_MATRIX");
                       for( int k=0;k<uniqueTerms.size();k++){
			for(int m=0;m<documents.size();m++){
			System.out.print(   TERM_DOCUMENT_MATRIX[k][m]+"   " );
                        } 
                       System.out.println(" ");
                       
	}// k for
}}
		
                
                
	



